<?php

class ConfigController extends Zend_Controller_Action {

    protected $_log;
    protected $doctrineContainer;

    public function init() {
        $this->_log = Zend_Registry::get('log');
        $this->doctrineContainer = Zend_Registry::get('doctrine');
    }

    public function indexAction() {
        
    }

    public function localeAction() {

        $this->view->headTitle('config', 'PREPEND');  

        $form = new Application_Form_ConfigDateTimeZone();
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $tzone = $form->getValue('tzone');
                $timeformat = $form->getValue('timeformat');
                $dateformat = $form->getValue('dateformat');
                $localization = $form->getValue('localization');

                $configpath = APPLICATION_PATH . '/configs/system.ini';
                $config = new Zend_Config_Ini($configpath,
                                null,
                                array('skipExtends' => true,
                                    'allowModifications' => true));

                $config->datetime->timezone = $tzone;
                $config->datetime->timeformat = $timeformat;
                $config->datetime->dateformat = $dateformat;
                $config->datetime->localization = $localization;

                $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => $configpath));
                $writer->write();
                $this->_redirect("config/locale");
            } else {
                $form->populate($formData);
            }
        }

        $this->view->form = $form;

        $dt = new DateTime("now");

        $dateTimeLocalization = Zend_Registry::get('dateTimeLocalization');
        $locale = new Zend_Locale($dateTimeLocalization->localization);
        $date = new Zend_Date($dt->getTimestamp(), false, $locale);
        $this->view->timezone = $date->getTimezone();
        $this->view->date = $date->toString($dateTimeLocalization->dateformat);
        $this->view->time = $date->toString($dateTimeLocalization->timeformat);
        
        $this->view->G = $date->toString('G');
        $this->view->GG = $date->toString('GG');
        $this->view->GGG = $date->toString('GGG');
        $this->view->GGGG = $date->toString('GGGG');
        $this->view->GGGGG = $date->toString('GGGGG');
        
        $this->view->MMM = $date->toString('MMM');
        $this->view->MMMM = $date->toString('MMMM');
        $this->view->MMMMM = $date->toString('MMMMM');
        
        $this->view->E = $date->toString('E');
        $this->view->EE = $date->toString('EE');
        $this->view->EEE = $date->toString('EEE');
        $this->view->EEEE = $date->toString('EEEE');
        $this->view->EEEEE = $date->toString('EEEEE');
        
        $this->view->a = $date->toString('a');
        
        $this->view->z = $date->toString('z');
        $this->view->zz = $date->toString('zz');
        $this->view->zzz = $date->toString('zzz');
        $this->view->zzzz = $date->toString('zzzz');

        $locss = new Zend_Locale();
        $locss->setLocale(Zend_Locale::BROWSER);
        $requeLang = key($locss->getBrowser());
        print_r($requeLang);
        
    }

}
